package com.payment.service;

import com.payment.entity.common.OrderPaymentManager;
import com.payment.entity.pojo.request.RequestBody.PayNotifyRequestBody;
import com.payment.entity.pojo.request.RequestHeader;
import com.payment.entity.pojo.request.subRequest.PayNotifyRequest;
import com.payment.entity.pojo.response.subResponse.PayNotifyResponse;
import com.payment.task.TurntoInterfaceUtils;
import com.payment.utils.Constant;
import com.payment.utils.HttpClientTool;
import com.payment.utils.JaxbUtil;

/**
 * 此类专门去做支付结果通知的业务处理，将返回的结果给回到controller继续处理
 */
public class PaymentResultService {
    public OrderPaymentManager payNotify(String orderId, String payType, String partnerCode, String orderCode, String deviceId, String deviceType, String payAmount, String secret) throws Exception {
        try {
            System.out.println("进到payNotify支付结果通知开始！" + orderId);
            //请求body参数处理
            PayNotifyRequestBody body = new PayNotifyRequestBody();
            body.setOrderId(orderId);
            body.setDeviceId(deviceId);
            body.setDeviceType(deviceType);
            body.setPayType(payType);
            body.setPayAmount(payAmount);
            //参数拼接
            PayNotifyRequest request = new PayNotifyRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_payNotify, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(PayNotifyRequest.class).toXml(request, "UTF-8");
            System.out.println("请求报文：" + sRequest);
            //String url = "http://localhost:80/TestService/test/payNotify";
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("响应报文：" + result);
            PayNotifyResponse responseObj = new JaxbUtil(PayNotifyResponse.class).fromXml(result, "utf-8");
            // 返回错误结果的处理
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("同步支付结果错误");
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            } else {
                // 当程序跑到这一步就真的完成了
                System.out.println("同步支付结果成功");
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

}
